package com.jemmyee.pizza.web.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jemmyee.core.dao.Page;
import com.jemmyee.pizza.entity.GasOut;
import com.jemmyee.pizza.service.IGasOutService;
import com.jemmyee.pizza.web.Constants;
import com.jemmyee.pizza.web.bean.CaiBean;


/**
 * @Description:
 * @author:jemmyee@gmail.com
 * @date:2010-10-28
 * @version:v1.0
 */
@Controller
@RequestMapping("/admin/gasout")
public class GasOutController {
	
	private static Log log= LogFactory.getLog(GasOutController.class);
	
	@Autowired
	private IGasOutService<GasOut> gasOutService;
	

	@RequestMapping("/to_add")
	public ModelAndView toAdd(){
		ModelAndView mv=new ModelAndView();
		mv.setViewName("/admin/gasout/add");
		return mv;
	}
	
	@RequestMapping("/to_edit/{id}")
	public ModelAndView toEdit(@PathVariable Integer id,HttpServletRequest request){
		   ModelAndView mv=new ModelAndView();
		   mv.addObject("gasout", gasOutService.findById(id));
		   mv.setViewName("/admin/gasout/edit");
		   return mv;
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public Map<String, Object> add(@RequestBody GasOut gasOut,HttpServletRequest request)
	{   
		Map<String, Object> model = new HashMap<String, Object>();
		if(false)
		{
			model.put("msg","error");
		}else
		{
			gasOut.setCreateTime(new Date());
			gasOut.setStatus((short)0);
			gasOutService.save(gasOut);
			model.put("msg","succ");
		}
		return model;
	}


	@RequestMapping("/edit")
	@ResponseBody
	public Map<String, Object> edit(@RequestBody GasOut gasOut,HttpServletRequest request)
	{   
		Map<String, Object> model = new HashMap<String, Object>();
		if(false)
		{
			model.put("msg","error");
		}else
		{
			GasOut temp=gasOutService.findById(gasOut.getId());
			temp.setRemark(gasOut.getRemark());
			temp.setXuhao(gasOut.getXuhao());
			temp.setGas_count(gasOut.getGas_count());
			temp.setKaipiaoren(gasOut.getKaipiaoren());
			temp.setMan(gasOut.getMan());
			temp.setMoney(gasOut.getMoney());
			temp.setPrice(gasOut.getPrice());
			gasOutService.update(temp);
			model.put("msg","succ");
		}
		return model;
	}

	
	@RequestMapping(value="/list/{from}/{startIndex}")
	public ModelAndView list(@PathVariable Integer startIndex,@PathVariable String from,@ModelAttribute CaiBean caibean,HttpServletRequest request){
		  DetachedCriteria dc= DetachedCriteria.forClass(GasOut.class);
		  CaiBean bean=new CaiBean();
		  if(from.equals("form"))
		   {
			   bean=caibean;
			   request.getSession().setAttribute("gas_out_bean", bean);
		   }
		   if(from.equals("page"))
		   {
			   if(request.getSession().getAttribute("gas_out_bean")!=null)
			   {
				   bean=(CaiBean)request.getSession().getAttribute("gas_out_bean");
			   }
		   }
		   if(from.equals("menu"))
		   {
			   
		   }
		 
//		   if(bean.getType()!=null&&bean.getType()!=0){
//			  dc.add(Restrictions.eq("type",bean.getType()));
//			}
		   //发布时间
			if (bean.getStartTime()!=null&&bean.getEndTime()!= null) {
				dc.add(Restrictions.ge("createTime",bean.getStartTime()));
				dc.add(Restrictions.le("createTime",bean.getEndTime()));
			}
			if (bean.getStartTime()!=null&&bean.getEndTime()== null) {
				dc.add(Restrictions.ge("createTime",bean.getStartTime()));
			}
			if (bean.getStartTime()==null&&bean.getEndTime()!= null) {
				dc.add(Restrictions.le("createTime",bean.getEndTime()));
			}   
		
//		   dc.addOrder(Order.desc("createTime"));
		   ModelAndView mv=new ModelAndView();
		   Page page=gasOutService.findPageByCriteria(dc,Constants.DEFAULT_PAGESIZE, startIndex,"createTime");
		   mv.addObject("page",page);
		   mv.addObject("bean",bean);
		   mv.setViewName("/admin/gasout/list");
		   return mv;
	}
	
	@RequestMapping(value="/delete/{id}")
	public String delete(@PathVariable Integer id,HttpServletRequest request){
		gasOutService.delete(gasOutService.findById(id));
	    return "redirect:/admin/gasout/list/menu/0";
	}

}
